package com.hope.hotel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hope.hotel.common.Result;
import com.hope.hotel.entity.po.Rooms;
import com.hope.hotel.mapper.RoomsMapper;
import com.hope.hotel.service.IRoomsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.Instant;

/**
 * <p>
 *  客房服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-30
 */
@Service
@RequiredArgsConstructor
public class RoomsServiceImpl extends ServiceImpl<RoomsMapper, Rooms> implements IRoomsService {

    /**
     * 根据酒店ID获取客房列表
     * @param hotelId 酒店ID
     * @param page 页码
     * @param pageSize 每页大小
     * @return 结果
     */
    @Override
    public Result getRoomsByHotelId(Long hotelId, int page, int pageSize) {
        if (hotelId == null) {
            return Result.error("酒店ID不能为空");
        }
        
        Page<Rooms> pageParam = new Page<>(page, pageSize);
        LambdaQueryWrapper<Rooms> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Rooms::getHotelId, hotelId);
        
        Page<Rooms> roomsPage = this.page(pageParam, queryWrapper);
        return Result.success(roomsPage);
    }

    /**
     * 根据ID获取客房详情
     * @param id 客房ID
     * @return 结果
     */
    @Override
    public Result getRoomById(Long id) {
        Rooms room = this.getById(id);
        if (room == null) {
            return Result.error("客房不存在");
        }
        return Result.success(room);
    }

    /**
     * 添加客房
     * @param room 客房信息
     * @return 结果
     */
    @Override
    public Result addRoom(Rooms room) {
        if (room.getHotelId() == null) {
            return Result.error("酒店ID不能为空");
        }
        
        boolean success = this.save(room);
        if (success) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 更新客房
     * @param room 客房信息
     * @return 结果
     */
    @Override
    public Result updateRoom(Rooms room) {
        if (room.getId() == null) {
            return Result.error("客房ID不能为空");
        }
        
        if (room.getHotelId() == null) {
            return Result.error("酒店ID不能为空");
        }
        
        boolean success = this.updateById(room);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除客房
     * @param id 客房ID
     * @return 结果
     */
    @Override
    public Result deleteRoom(Long id) {
        boolean success = this.removeById(id);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
}
